﻿using DevExpress.XtraEditors;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Shopfloormanagement
{
    public partial class Step4 : DevExpress.XtraEditors.XtraForm
    {
        public Step4()
        {
            InitializeComponent();
        }
        int step1;
        int step2;
        int step3;
        int step4;
        int step5;
        int step6;
        int step7;
        int step8;
        int step9;
        private void LoadA3list()//获取上方图片状态
        {

            DataTable A3list = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");

            step1 = (int)A3list.Rows[0][6];
            step2 = (int)A3list.Rows[0][7];
            step3 = (int)A3list.Rows[0][8];
            step4 = (int)A3list.Rows[0][9];
            step5 = (int)A3list.Rows[0][10];
            step6 = (int)A3list.Rows[0][11];
            step7 = (int)A3list.Rows[0][12];
            step8 = (int)A3list.Rows[0][13];
            step9 = (int)A3list.Rows[0][14];
            LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
        }
        private void LoadPicture(int condition1, int condition2, int condition3, int condition4, int condition5, int condition6, int condition7, int condition8, int condition9)
        {
            switch (condition1)
            {
                case 1:
                    picstep1.Image = Properties.Resources.G1;
                    break;
                case 2:
                    picstep1.Image = Properties.Resources.B1;
                    break;
                case 3:
                    picstep1.Image = Properties.Resources.R1;
                    break;
                case 4:
                    picstep1.Image = Properties.Resources.H1;
                    break;
                default:
                    picstep1.Image = null;
                    break;
            }
            switch (condition2)
            {
                case 1:
                    picstep2.Image = Properties.Resources.G2;
                    break;
                case 2:
                    picstep2.Image = Properties.Resources.B2;
                    break;
                case 3:
                    picstep2.Image = Properties.Resources.R2;
                    break;
                case 4:
                    picstep2.Image = Properties.Resources.H2;
                    break;
                default:
                    picstep2.Image = null;
                    break;
            }
            switch (condition3)
            {
                case 1:
                    picstep3.Image = Properties.Resources.G3;
                    break;
                case 2:
                    picstep3.Image = Properties.Resources.B3;
                    break;
                case 3:
                    picstep3.Image = Properties.Resources.R3;
                    break;
                case 4:
                    picstep3.Image = Properties.Resources.H3;
                    break;
                default:
                    picstep3.Image = null;
                    break;
            }
            switch (condition4)
            {
                case 1:
                    picstep4.Image = Properties.Resources.G4;
                    break;
                case 2:
                    picstep4.Image = Properties.Resources.B4;
                    break;
                case 3:
                    picstep4.Image = Properties.Resources.R4;
                    break;
                case 4:
                    picstep4.Image = Properties.Resources.H4;
                    break;
                default:
                    picstep4.Image = null;
                    break;
            }
            switch (condition5)
            {
                case 1:
                    picstep5.Image = Properties.Resources.G5;
                    break;
                case 2:
                    picstep5.Image = Properties.Resources.B5;
                    break;
                case 3:
                    picstep5.Image = Properties.Resources.R5;
                    break;
                case 4:
                    picstep5.Image = Properties.Resources.H5;
                    break;
                default:
                    picstep5.Image = null;
                    break;
            }
            switch (condition6)
            {
                case 1:
                    picstep6.Image = Properties.Resources.G6;
                    break;
                case 2:
                    picstep6.Image = Properties.Resources.B6;
                    break;
                case 3:
                    picstep6.Image = Properties.Resources.R6;
                    break;
                case 4:
                    picstep6.Image = Properties.Resources.H6;
                    break;
                default:
                    picstep6.Image = null;
                    break;
            }
            switch (condition7)
            {
                case 1:
                    picstep7.Image = Properties.Resources.G7;
                    break;
                case 2:
                    picstep7.Image = Properties.Resources.B7;
                    break;
                case 3:
                    picstep7.Image = Properties.Resources.R7;
                    break;
                case 4:
                    picstep7.Image = Properties.Resources.H7;
                    break;
                default:
                    picstep7.Image = null;
                    break;
            }
            switch (condition8)
            {
                case 1:
                    picstep8.Image = Properties.Resources.G8;
                    break;
                case 2:
                    picstep8.Image = Properties.Resources.B8;
                    break;
                case 3:
                    picstep8.Image = Properties.Resources.R8;
                    break;
                case 4:
                    picstep8.Image = Properties.Resources.H8;
                    break;
                default:
                    picstep8.Image = null;
                    break;
            }
            switch (condition9)
            {
                case 1:
                    picstep9.Image = Properties.Resources.G9;
                    break;
                case 2:
                    picstep9.Image = Properties.Resources.B9;
                    break;
                case 3:
                    picstep9.Image = Properties.Resources.R9;
                    break;
                case 4:
                    picstep9.Image = Properties.Resources.H9;
                    break;
                default:
                    picstep9.Image = null;
                    break;
            }

        }

        private void Word()//加载进度下方文字
        {
            lblStep1.Text = "Problem\nStatement";
            lblStep2.Text = "Problem\nDescription";
            lblStep3.Text = "Interim\nContainment\nactions";
            lblStep4.Text = "Identify\nPossible\nCauses";
            lblStep5.Text = "Test all\nPossible\nCauses";
            lblStep6.Text = "Verify\nPotential\nRoot Cause";
            lblStep7.Text = "Confirmed\nRoot Cause";
            lblStep8.Text = "Permanent\nCorrective\nActions";
            lblStep9.Text = "Further\nActivities";

        }
        private void LoadA3step4()//加载信息及判断是否过期
        {
            DataTable Fish1 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
            Fishbone.Text = Fish1.Rows[0][4].ToString();
            DataTable Fish = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step4", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (Fish.Rows.Count == 0) return; // 如果没有数据，直接返回
            DataRow row = Fish.Rows[0]; // 取第 0 行数据
            // 加载Man
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Man{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m1{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Machine
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Machine{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m2{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Material
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Material{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m3{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Method
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Method{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m4{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Mileu
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Mileu{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m5{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Measurement
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Measurement{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m6{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }


            //获取选中A3Step1的数据(Duedate)
            DataTable A3Step1 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step1", $"A3ReportNumberID = '{rootcause.A3number}'");
            //超期图片变红
            object dbTimeObj = A3Step1.Rows[0]["Duedate"];//获取数据库中的日期数据
            DateTime dbTime;
            if (dbTimeObj != DBNull.Value && DateTime.TryParse(dbTimeObj.ToString(), out dbTime))
            {
                if (dbTime < DateTime.Now)
                {
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step4 = 3", $"ID = '{rootcause.A3number}' and Step4 != 1");
                    //Load A3list 获得step3的值
                    DataTable A3listTemp = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
                    //获取上方图片状态
                    step1 = (int)A3listTemp.Rows[0][6];
                    step2 = (int)A3listTemp.Rows[0][7];
                    step3 = (int)A3listTemp.Rows[0][8];
                    step4 = (int)A3listTemp.Rows[0][9];
                    step5 = (int)A3listTemp.Rows[0][10];
                    step6 = (int)A3listTemp.Rows[0][11];
                    step7 = (int)A3listTemp.Rows[0][12];
                    step8 = (int)A3listTemp.Rows[0][13];
                    step9 = (int)A3listTemp.Rows[0][14];
                    LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
                }
                else
                {
                    return;
                }
            }
        }
        private void Step4_Load(object sender, EventArgs e)
        {
            LoadA3list();
            Word();
            LoadA3step4();
            palstep1.BackColor = Color.FromArgb(146, 208, 80);
            palstep2.BackColor = Color.FromArgb(146, 208, 80);
            palstep3.BackColor = Color.FromArgb(146, 208, 80);
            palstep4.BackColor = Color.FromArgb(146, 208, 80);


        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            DataTable Step4 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step4", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (Step4.Rows.Count > 0)
            {
                Step5 frm = new Step5();
                frm.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("请进行鱼骨图分析", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            
            Step3 frm = new Step3();
            frm.Show();
            this.Close();

        }

        private void picFish_Click(object sender, EventArgs e)
        {
            this.Close();
            Fish frm = new Fish();
            frm.Show();
        }

        private void btnClosed_Click(object sender, EventArgs e)
        {
            // 弹出确认对话框，询问是否关闭
            DialogResult result = MessageBox.Show("是否确定关闭？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                // 用户选择“是”，执行关闭操作
                this.Close(); // 关闭当前窗体
            }
            else
            {
                // 用户选择“否”，返回，不做任何操作
                return;
            }
        }
        //点击图片跳转功能
        private void picstep1_Click(object sender, EventArgs e)
        {
            if (step1 == 4)
            {
                return;
            }
            else
            {
                Step1 frm = new Step1();
                this.Close();
                frm.Show();
            }
        }

        private void picstep2_Click(object sender, EventArgs e)
        {
            if (step2 == 4)
            {
                return;
            }
            else
            {
                Step2 frm = new Step2();
                this.Close();
                frm.Show();
            }
        }

        private void picstep3_Click(object sender, EventArgs e)
        {
            if (step3 == 4)
            {
                return;
            }
            else
            {
                Step3 frm = new Step3();
                this.Close();
                frm.Show();
            }
        }
        private void picstep4_Click(object sender, EventArgs e)
        {
            if (step4 == 4)
            {
                return;
            }
            else
            {
                Step4 frm = new Step4();
                this.Close();
                frm.Show();
            }
        }

        private void picstep5_Click(object sender, EventArgs e)
        {
            if (step5 == 4)
            {
                return;
            }
            else
            {
                Step5 frm = new Step5();
                this.Close();
                frm.Show();
            }
        }

        private void picstep6_Click(object sender, EventArgs e)
        {
            if (step6 == 4)
            {
                return;
            }
            else
            {
                Step6 frm = new Step6();
                this.Close();
                frm.Show();
            }
        }

        private void picstep7_Click(object sender, EventArgs e)
        {
            if (step7 == 4)
            {
                return;
            }
            else
            {
                Step7 frm = new Step7();
                this.Close();
                frm.Show();
            }
        }

        private void picstep8_Click(object sender, EventArgs e)
        {
            if (step8 == 4)
            {
                return;
            }
            else
            {
                Step8 frm = new Step8();
                this.Close();
                frm.Show();
            }
        }

        private void picstep9_Click(object sender, EventArgs e)
        {
            if (step9 == 4)
            {
                return;
            }
            else
            {
                Step9 frm = new Step9();
                this.Close();
                frm.Show();
            }
        }

        private void picFish_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}